package util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaTransactionManager;

public class EMFUtil extends JpaTransactionManager {

	/**
	 */
	private static final long serialVersionUID = 1L;
	
	private static JpaTransactionManager transactionManager;
	
	@Autowired
	private EntityManagerFactory entityManagerFactory;
	
	public static EntityManager getEM(){
		
		/*ApplicationContext ctx =  new ClassPathXmlApplicationContext("../DispatcherServlet-servlet.xml");
		EntityManagerFactory emf = (EntityManagerFactory) ctx.getBean("entityManagerFactory");
		System.out.println(emf);
		List<User> list = emf.createEntityManager().createNamedQuery("User.findAll",User.class).getResultList();
		System.out.println(list);*/
		
		/*System.out.println("entityManagerFactory:"+entityManagerFactory);
		System.out.println("transactionManager："+transactionManager);*/
		System.out.println("EM#:"+new EMFUtil().entityManagerFactory);
		return transactionManager.getEntityManagerFactory().createEntityManager();
		
	}
	public static JpaTransactionManager getTXM(){
		return transactionManager;
	}

	public static void setTransactionManager(JpaTransactionManager transactionManager) {
		EMFUtil.transactionManager = transactionManager;
	}
	
/*	public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
		this.entityManagerFactory = entityManagerFactory;
	}
*/
	
	
	
}
